package com.itheima.health.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.itheima.health.common.JacksonObjectMapper;
import com.itheima.health.interceptor.LoginInterCeptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
public class WebConfig  extends WebMvcConfigurationSupport {
    @Autowired
    private LoginInterCeptor loginInterCeptor;

    /**
     * 免登陆访问
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //super.addResourceHandlers(registry);
        registry.addResourceHandler("/backend/**")
                .addResourceLocations("classpath:/static/backend/");

        registry.addResourceHandler("/index.html")
                .addResourceLocations("classpath:/static/index.html");

//        registry.addResourceHandler("/backend")
//                .addResourceLocations("classpath:/static/backend/index.html");

        //前台放行
        //registry.addResourceHandler("/front/**").addResourceLocations("classpath:/static/front/");
    }

    //分页
    @Bean
    public MybatisPlusInterceptor page(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }

    /**
     * 拦截器
     * @param registry
     */
//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(loginInterCeptor).addPathPatterns("/employee","/employee/*");
//        super.addInterceptors(registry);
//    }

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {

        super.addViewControllers(registry);
    }


    //消息转换器

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter m2j = new MappingJackson2HttpMessageConverter();
        m2j.setObjectMapper(new JacksonObjectMapper());

        converters.add(0,m2j);
        super.extendMessageConverters(converters);
    }
}
